<%-- 
    Document   : getDepartures
    Created on : Jul 30, 2009, 1:52:32 PM
    Author     : david
--%>


<%@page contentType="text/html" pageEncoding="UTF-8"
        import="com.appspot.nnaplanner.*,
        java.util.List,
        javax.jdo.Query,
        org.jgrapht.graph.*,
        org.jgrapht.alg.*,
        com.appspot.nnaplanner.DAL.*,
        com.google.appengine.api.datastore.Key,
        com.google.appengine.api.datastore.KeyFactory"
        %>


<%
            DALConnection dc = new DALConnection();
            int count = 0;
//if (request.getMethod() != "POST") {
            String id = request.getParameter("id");
            String action = request.getParameter("action");

            if (action.equals("times")) {
                dc = dc.getByID(request.getParameter("id"));
                int total = 0;
                if (dc.getTimes() != null) {
                    total = dc.getTimes().size();
                    count = 0;
                    out.print("{\"total\": \"" + total + "\", \"times\":[");
                    for (int i = 0; i < dc.getTimes().size(); i++) {
                        if (count > 0) {
                            out.print(", ");
                        }
                        out.print("{");
                        out.print("\"time\": \"" + dc.getTimes().get(i).toString() + "\", ");
                        out.print("\"duration\": \"" + dc.getDurations().get(i).toString() + "\", ");
                        out.print("\"desc\": \"" + dc.getDescriptions().get(i).toString() + "\", ");
                        out.print("}");
                        count++;
                    }
                    out.print("] }");
                } else {
                    out.print("{\"total\":\"" + total + "\", \"conid:\"" + dc.getID() + "\", \"times\":[{\"time:\"01/01/1970 00:00\", duration:\"0\", desc:\"No departures\"}] }");
                }

           }

            if (action.equals("conns")) {

                List<DALConnection> conns = dc.getConnectionsFor(id);

                out.print("{\"total\": \"" + conns.size() + "\", \"departures\":[");
                count = 0;
                for (DALConnection d : conns) {
                    if (count > 0) {
                        out.print(", ");
                    }
                    out.print("{");
                    out.print("\"conid\": \"" + d.getID() + "\", ");
                    out.print("\"stopid\": \"" + d.getStopID() + "\", ");
                    out.print("\"sname\": \"" + d.getStopName() + "\", ");
                    out.print("\"depid\": \"" + d.getDestinationID() + "\", ");
                    out.print("\"dname\": \"" + d.getDestinationName() + "\", ");
                    if (d.getTimes() != null) {
                        out.print("\"departures\": \"" + d.getTimes().size() + "\", ");
                    } else {
                        out.print("\"departures\": \"0\"");
                    }
                    out.print("}");
                    count++;
                }
                out.print("] }");



            }
            //}

%>
